package com.ruoyi.stock2.thread;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.stock2.domain.DownTradedataSchedule;
import com.ruoyi.stock2.domain.STOCK;
import com.ruoyi.stock2.domain.TRADEDATA;
import com.ruoyi.stock2.domain.TRADEDATA200;
import com.ruoyi.stock2.service.ISTOCKService;
import com.ruoyi.stock2.service.ITRADEDATA200Service;
import com.ruoyi.stock2.service.ITRADEDATAService;
import com.ruoyi.stock2.utils.DateUtils2;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ToTradeData200 implements Runnable{
    ITRADEDATAService itradedataService;
    ITRADEDATA200Service itradedata200Service;
    ISTOCKService istockService;
    boolean working = false;

    public void setWorking(boolean working) {
        this.working = working;
    }

    public boolean isWorking() {
        return working;
    }

    public ITRADEDATA200Service getItradedata200Service() {
        return (ITRADEDATA200Service) SpringUtils.getBean(ITRADEDATA200Service.class);
    }

    public ISTOCKService getIstockService() {
        return (ISTOCKService) SpringUtils.getBean(ISTOCKService.class);
    }

    public ITRADEDATAService getItradedataService() {
        return (ITRADEDATAService) SpringUtils.getBean(ITRADEDATAService.class);
    }

    //单例实现
    public static ToTradeData200 getInstance() {
        return ToTradeData200.Singtonle.instance;
    }

    private static class Singtonle {
        private static ToTradeData200 instance = new ToTradeData200();
    }

    @Override
    public void run() {
        itradedata200Service = getItradedata200Service();
        istockService = getIstockService();
        itradedataService = getItradedataService();

        List<STOCK> stocks = istockService.selectSTOCKList(null);//from stock order by SYMBOL ASC
        ExecutorService executorMailAna = new ThreadPoolExecutor(10,
                10, 4L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1000),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (STOCK stock : stocks) {
            executorMailAna.execute(new Runnable() {
                @Override
                public void run() {

                    long a = System.currentTimeMillis();
                    TRADEDATA tradeDataKey = new TRADEDATA(stock.getSymbol(),
                            DateUtils2.formatStringToDate("2004-01-01", "yyyy-MM-dd"));
                    List<TRADEDATA> list = itradedataService.get200ByKeyDateTimeDesc(tradeDataKey);
                    long b = System.currentTimeMillis();

                    List<TRADEDATA200> list200 = new ArrayList<TRADEDATA200>(30);
                    int sizec = list.size()-1;
                    for (int i = sizec; i >= 0; i--) {
                        TRADEDATA200 tradedata200 = new TRADEDATA200();
                        BeanUtils.copyProperties(list.get(i), tradedata200);
                        list200.add(tradedata200);
                        if(list200.size()==30){
                            itradedata200Service.insertTRADEDATA200b(list200);
                            list200.clear();
                        }
                    }
                    if(list200.size()>0){
                        itradedata200Service.insertTRADEDATA200b(list200);
                        list200.clear();
                    }
                    long c = System.currentTimeMillis();
                    long l1 = (b-a);
                    long l2 = (c-b)/1000;
                    System.out.println(stock.getSymbol() + ", 查询耗时：" + l1 + "，插入耗时：" + l2);

                }
            });

        }
        System.out.println("等待着1");
        executorMailAna.shutdown();
        while (!executorMailAna.isTerminated()) {
            try {
                executorMailAna.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("等待着2");

    }



}
